package com.doublefish.doublefishrobot.ui.home.s8;

import android.app.Dialog;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.base.BaseActivity;
import com.doublefish.doublefishrobot.ui.home.DeviceCheckActivity;
import com.doublefish.doublefishrobot.ui.home.bean.DeviceBean;
import com.doublefish.doublefishrobot.utils.ble.DFBleManager;
import com.doublefish.doublefishrobot.widget.CommonDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者:dugaofeng on 2024/10/18 16:10
 */
public class S8ModeMenuActivty extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView titleTV;

    @Override
    protected int getLayoutId() {
        return R.layout.activty_s8_menu;
    }

    @Override
    protected void initView() {
        titleTV.setText(R.string.super_8_robot);
        DeviceBean deviceBean = DFBleManager.getInstance().getBleDeviceBean();
        if (deviceBean != null) {
            String title = deviceBean.getAlias();
            if (!title.isEmpty()) {
                titleTV.setText(title);
            }
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    private void toS8ModeNormalActivity() {
        Intent intent = new Intent(this, S8ModeNormalActivity.class);
        startActivity(intent);
    }

    private void toS8ModeTrainListActivity() {
        Intent intent = new Intent(this, S8ModeTrainListActivity.class);
        startActivity(intent);
    }

    private void toS8ModeCustomListActivity() {
        Intent intent = new Intent(this, S8ModeCustomListActivity.class);
        startActivity(intent);
    }

    @OnClick({R.id.tv_mode_normal, R.id.tv_mode_train, R.id.tv_mode_custom, R.id.tv_init_check})
    public void onItemClick(View view) {
        switch (view.getId()) {
            case R.id.tv_mode_normal:
                toS8ModeNormalActivity();
                break;
            case R.id.tv_mode_train:
                toS8ModeTrainListActivity();
                break;
            case R.id.tv_mode_custom:
                toS8ModeCustomListActivity();
                break;

            case R.id.tv_init_check:
                toInitCheckActivity();
                break;

        }
    }

    private void toInitCheckActivity() {
        DeviceBean deviceBean = DFBleManager.getInstance().getBleDeviceBean();
        Intent intent = DeviceCheckActivity.makeIntent(this, deviceBean);
        startActivity(intent);
    }

    @OnClick(R.id.ll_back)
    public void onBackClick() {
        showBackAlert();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            showBackAlert();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void showBackAlert() {
        //询问连接新设备
        String title = getString(R.string.disconnect_alert);
        CommonDialog dialog = new CommonDialog(this, title, null, new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    disconnectBle();
                    finish();
                }
            }
        });
        dialog.setPositiveButton(getString(R.string.disconnect));
        dialog.show();
    }

    private void disconnectBle() {
        DFBleManager.getInstance().disConnect();
    }
}
